/* INCLUSIONS */
#include "TIMER.h"

/* FUNCTION BODIES */
void TIMER_INIT(LPC_TIM_TypeDef* TIMER, uint32_t PRESCALE_VALUE) {

  TIM_TIMERCFG_Type TIMCFG;
  TIMCFG.PrescaleOption = TIM_PRESCALE_USVAL;
  TIMCFG.PrescaleValue = PRESCALE_VALUE;

  TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &TIMCFG);
}

void TIMER_MATCH_CFG(TIM_MATCHCFG_Type* MCFG,LPC_TIM_TypeDef* TIMER, 
                     uint32_t MVALUE) {
  PINSEL_CFG_Type PINCFG;
  PINCFG.Funcnum = PINSEL_FUNC_3;
  PINCFG.OpenDrain = PINSEL_PINMODE_NORMAL;
  PINCFG.Pinmode = PINSEL_PINMODE_PULLUP;
  PINCFG.Portnum = PINSEL_PORT_1;
  PINCFG.Pinnum = 28;

  PINSEL_ConfigPin(&PINCFG);

  MCFG->ExtMatchOutputType = TIM_EXTMATCH_NOTHING;
  MCFG->IntOnMatch = ENABLE;
  MCFG->MatchChannel = 0;
  MCFG->MatchValue = MVALUE;
  MCFG->ResetOnMatch = ENABLE;
  MCFG->StopOnMatch = DISABLE;
  TIM_ConfigMatch(LPC_TIM0, MCFG);
}

void TIMER_MVALUE_EDIT(TIM_MATCHCFG_Type* CFG, uint32_t VALUE) {
  CFG -> MatchValue = VALUE;
  TIM_ConfigMatch(LPC_TIM0, CFG);
}

void TIMER_ENABLE(LPC_TIM_TypeDef * TIMER) {
  TIM_Cmd(TIMER, ENABLE);
}

void TIMER_DISABLE(LPC_TIM_TypeDef * TIMER) {
  TIM_Cmd(TIMER, DISABLE);
}
